package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Handler;

/**
 * @author jack
 * @data 2024 14:55
 */
@Service
@Transactional
@Slf4j
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {


    /**
     * app用户登录
     *
     * @param loginDto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {
        //判断是否为游客登录
        if (StringUtils.isEmpty(loginDto.getPhone()) && StringUtils.isEmpty(loginDto.getPassword()) ){
            //1. 生成token
            String token = AppJwtUtil.getToken(0);

            //3. 进行返回数据的封装
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("token", token);
            return ResponseResult.okResult(resultMap);
        }else {
            //1. 先根据用户名查询用户信息
            LambdaQueryWrapper<ApUser> queryWrapper = Wrappers
                    .<ApUser>lambdaQuery()
                    .eq(ApUser::getPhone, loginDto.getPhone());
            ApUser apUser = getOne(queryWrapper);

            //2. 用户校验
            //2.1 校验手机号
            if (apUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ACCOUNT_PASSWORD_ERROR);
            }
            //2.2 校验密码： MD5(前端传的密码+ 盐) 和数据库 密码比较
            String inputPasswordWithMD5 = DigestUtils.md5DigestAsHex((loginDto.getPassword() + apUser.getSalt()).getBytes(StandardCharsets.UTF_8));
            if (inputPasswordWithMD5.equals(apUser.getPassword())) {
                //2.3 比较成功：生成token，返回用户信息和token
                //1. 生成token
                String token = AppJwtUtil.getToken(apUser.getId());
                //2. 处理用户信息
                apUser.setPassword(null);
                apUser.setSalt(null);
                //3. 进行返回数据的封装
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("user", apUser);
                resultMap.put("token", token);
                return ResponseResult.okResult(resultMap);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ACCOUNT_PASSWORD_ERROR);
            }
        }
    }
}
